package com.manager.web.system;

import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.manager.base.BaseAction;
import com.manager.pojo.system.SysDepartment;
import com.manager.util.CONSTANT;
import com.manager.util.DateUtil;
import com.manager.util.Log;
import com.opensymphony.xwork2.Action;

public class SysDepartmentAction extends BaseAction {
	private SysDepartment sysDepartment;
	
	/*
	 * 添加左边栏部门
	 */
	public String addDepartment(){
		int loginUserId = super.getLoginUserId();
		PrintWriter p = null;
		
		try{
			p = response.getWriter();
			
			//查询部门名是否已存在
			int total = super.baseService.queryInt("select count(1) from sys_department where name = ?", sysDepartment.getName());
			if(total == 1){
				p.print("{\"flag\":false,\"msg\":\"该部门名已存在\"}");
			}else{
				SysDepartment sd = new SysDepartment();
				sd.setName(sysDepartment.getName());
				sd.setCreateById(loginUserId);
				sd.setCreateDateTime(DateUtil.getCurrentDateTime(true));
				sd.setUpdateById(loginUserId);
				sd.setUpdateDateTime(DateUtil.getCurrentDateTime(true));
				super.baseService.save(sd);
			
				p.print("{\"flag\":true}");
			}
		}catch(Exception e){
			p.print("{\"flag\":false}");
			e.printStackTrace();
		}finally{
			p.flush();
			p.close();
		}
		
		return Action.NONE;
	}
	
	/*
	 * 删除部门及子部门
	 */
	public String deleteDepartment(){
		int sysDepartmentId = sysDepartment.getId();
		PrintWriter p = null;
		
		try{
			p = response.getWriter();
			
			//永远不允许删除"临时部门"
			if(sysDepartmentId == 2){
				p.write("{\"flag\":false,\"msg\":\"永远不允许删除--临时部门\"}");
			}else{
				//删除该菜单
				super.baseService.delete("delete from sys_department where id = ?", sysDepartmentId);
				//将该部门其下的所有人员的department_id置为2(临时部门)
				super.baseService.update("update sys_user set department_id = 2 where department_id = ?", sysDepartmentId);
				
				p.write("{\"flag\":true}");
			}
		}catch(Exception e){
			p.write("{\"flag\":false}");
			e.printStackTrace();
		}finally{
			p.flush();
			p.close();
		}
		return Action.NONE;
	}
	
	/*
	 * 由id查询department
	 */
	public String queryDepartmentById(){
		int sysDepartmentId = sysDepartment.getId();
		String hql = "FROM SysDepartment m WHERE m.id = ?";
		SysDepartment sysDepartment = (SysDepartment)super.baseService.getObject(SysDepartment.class, sysDepartmentId);
		this.sysDepartment = sysDepartment;
		super.forward("system/departmentUpdate.jsp");
		
		return Action.NONE;
	}
	
	/*
	 * 更新department
	 */
	public String updateDepartment(){
		PrintWriter p = null;
		
		try{
			p = response.getWriter();
			
			//查询除了该ID的部门外，其它的所有部门中该部门名是否存在
			int total = super.baseService.queryInt("select count(1) from sys_department where name = ? and id != ?", sysDepartment.getName(), sysDepartment.getId());
			if(total == 1){
				p.print("{\"flag\":false,\"msg\":\"该部门名已存在\"}");
			}else{
				SysDepartment sd = (SysDepartment)super.baseService.getObject(SysDepartment.class, sysDepartment.getId());
				sd.setName(sysDepartment.getName());
				sd.setUpdateById(super.getLoginUserId());
				sd.setUpdateDateTime(DateUtil.getCurrentDateTime(true));
				super.baseService.update(sd);
				
				p.print("{\"flag\":true}");
			}
		}catch(Exception e){
			p.print("{\"flag\":false}");
			e.printStackTrace();
		}finally{
			p.flush();
			p.close();
		}
		
		return Action.NONE;
	}
	
	/*
	 * 查询所有的department，提供给jquery filetree使用
	 */
	public String queryDepartmentAll(){
		String root = request.getParameter("jqueryFiletreeId");
		PrintWriter p = null;
		StringBuffer sb = new StringBuffer();
		String sql = null;
		
		try{
			p = response.getWriter();
			
			if(root.equals("source")){
				sql = "select id, name from sys_department order by id desc";
			}else{
				p.flush();
				p.close();
				return Action.NONE;
			}
			
			List list = this.baseService.queryList(sql);
			
			if(list.size() > 0){
				sb.append("<ul class=\"jqueryFileTree\" style=\"display: none;\">");
				for(int i = 0; i < list.size(); i++){
					Map m = (Map)list.get(i);
					
					sb.append("<li class=\"directory collapsed\">");
						sb.append("<input type=\"checkbox\" style=\"display:none;\" value=\"" + m.get("id").toString() + "\"/>");
						sb.append("<a href=\"#\" rel=\"" + m.get("id").toString() + "\">" + m.get("name").toString() + "</a>");
					sb.append("</li>");
					
				}
				sb.append("</ul>");
				Log.info("menuAll:" + sb.toString());
				p.print(sb.toString());
			}else{
				p.print("there is no department!!");
			}
			
		}catch(Exception e){
			p.print("error!!");
			e.printStackTrace();
		}finally{
			p.flush();
			p.close();
		}
		
		return Action.NONE;
	}
	
	/*
	 * 查询所有的department，提供给下拉框使用
	 */
	public String queryMenuAll2(){
		PrintWriter p = null;
		StringBuffer sb = new StringBuffer();
		
		try{
			p = response.getWriter();
			
			List<Map> list = super.baseService.queryList("select id, name from sys_department order by id desc");
			Map m = null;
			
			sb.append("<option value=\"\">全部</option>");
			if(list != null && list.size() > 0){
				for(int i = 0; i < list.size(); i++){
					m = list.get(i);
					sb.append("<option value=\"").append(m.get("id").toString()).append("\">");
					sb.append(m.get("name").toString());
					sb.append("</option>");
				}
			}
			
			p.print(sb.toString());
		}catch(Exception e){
			p.print("error!!");
			e.printStackTrace();
		}finally{
			p.flush();
			p.close();
		}
		
		return Action.NONE;
	}
	
	/*
	 * getter and setter
	 */
	public SysDepartment getSysDepartment() {
		return sysDepartment;
	}
	
	public void setSysDepartment(SysDepartment sysDepartment) {
		this.sysDepartment = sysDepartment;
	}
}
